package huixue.cn.com.rxapp.rx;

import android.view.View;
import android.widget.Button;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;
import huixue.cn.com.rxapp.rx.entity.Data;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;

public class RxContactActivity extends SimpleActivity {

    @BindView(R.id.btn_rx_contact_event)
    Button btnRxContactEvent;

    @Override
    public int getLayout() {
        return R.layout.activity_rx_contact;
    }

    @Override
    public void initView() {

    }

    @Override
    public void addEvent() {

    }

    @OnClick({R.id.btn_rx_contact_event})
    public void onViewClicked(View view) {
        /*Sources sources = new Sources();
        //Create our sequence for querying best available data
        Observable<Data> source = Observable.concat(
                sources.memory(),
                sources.disk(),
                sources.network()
        ).first(data -> data != null && data.isUpToDate());
        // "Request" latest data once a second
        Observable.interval(1, TimeUnit.SECONDS)
                .flatMap(__ -> source)
                .subscribe(data -> System.out.println("Received: " + data.value));

        // Occasionally clear memory (as if app restarted) so that we must go to disk
        Observable.interval(3, TimeUnit.SECONDS)
                .subscribe(__ -> sources.clearMemory());

        // Java will quit unless we idle
        sleep(15 * 1000);*/

        Observable.zip(Observable.create(e -> {
            for (int i = 0; ; i++) { //一直不停的发
                e.onNext(i);
            }
        }).subscribeOn(Schedulers.newThread()),Observable.create(e->{
             for (int i = 0; i < 5; i++) {
                 e.onNext(0); //这里技巧性的处理：发1个0过去
             }
        }).subscribeOn(Schedulers.newThread()),
                (BiFunction<Object, Object, Object>) (i1, i2) -> (Integer) i1 + (Integer) i2) //1个数字+0，不影响原值
                .subscribe(integer -> System.out.println(integer));
        try{
            Thread.sleep(200);
        }catch (Exception e){

        }
    }

    static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        }
        catch (InterruptedException e) {
            // Ignore
        }
    }

}
